package com.dianrui.fmi.activity;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.adapter.EditStockAdapter;
import com.dianrui.fmi.bean.Optional;
import com.dianrui.fmi.event.RfreshIndex;
import com.dianrui.fmi.event.RfreshQutoes;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.dianrui.fmi.view.draglistview.DragSortListView;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */

public class EditStockActivity extends BaseActivity {

    private String chooseType;
    private EditStockAdapter editStockAdapter;
    private List<Optional> mDatas = new ArrayList<>();
    private List<String> symbolList = new ArrayList<>();
    private List<String> symbolListAll = new ArrayList<>();
    private String name;

    @BindView(R.id.emptyView)
    LinearLayout emptyView;
    @BindView(R.id.edit_stock_bottom_layout)
    LinearLayout editStockBottomLayout;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.clause_choose_layout)
    TextView clauseChooseLayout;
    @BindView(R.id.edit_listview)
    DragSortListView editListview;
    @BindView(R.id.clause_choose)
    CheckBox allChooseCb;
    @BindView(R.id.top_right)
    TextView editTxt;


    @Override
    public int getContentViewId() {
        return R.layout.activity_edit;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.modify));
        editTxt.setText(getString(R.string.edit));
        editTxt.setVisibility(View.VISIBLE);
        editStockAdapter = new EditStockAdapter(this, mDatas);
        editListview.setAdapter(editStockAdapter);
        editListview.setDragViewId(R.id.drag_layout);//要拖拽的控件id
        getOptional();
        editStockAdapter.setOnButtonClickListener(new EditStockAdapter.EditStockClickListener() {

            @Override
            public void onTop(int position) {
                mDatas.add(0, mDatas.get(position));
                // 置顶后list.size增加一 所以要position+1
                mDatas.remove(position + 1);
                editListview.smoothScrollToPosition(0);
                editStockAdapter.notifyDataSetChanged();
            }

            @Override
            public void getName(int pos, String names) {
                chooseType = "1";
                if (!symbolList.contains(names)) {
                    symbolList.add(names);
                } else {
                    symbolList.remove(names);
                }
                Log.e("单选的长度是", symbolList.size() + "");
                editStockAdapter.notifyDataSetChanged();
            }
        });
    }

    private void getOptional() {
        OkHttpUtils.getInstance().postWithMemberToken(Url.OPTIONAL, "", new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (mDatas != null) {
                                    mDatas.clear();
                                }
                                JSONArray data = jsonObject.optJSONArray("data");
                                if (data.length() > 0) {
                                    for (int i = 0; i < data.length(); i++) {
                                        Optional optional = new Optional();
                                        optional.full_symbol = data.optJSONObject(i).optString("full_symbol");
                                        optional.sort = data.optJSONObject(i).optString("sort");
                                        mDatas.add(optional);
                                    }
                                    if (editStockAdapter != null) {
                                        editStockAdapter.refresh(mDatas);
                                    }
                                    editListview.setVisibility(View.VISIBLE);
                                    emptyView.setVisibility(View.GONE);
                                } else {
                                    emptyView.setVisibility(View.VISIBLE);
                                    editListview.setVisibility(View.GONE);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    @OnClick({R.id.back, R.id.clause_choose,R.id.select_all_layout, R.id.delete_txt, R.id.top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.clause_choose://checkbox
                chooseType = "2";

                if (!editStockAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        if (mDatas.get(i).isCheck) {
                            mDatas.get(i).isCheck = false;
                            allChooseCb.setChecked(false);
                            if (symbolList != null) {
                                symbolList.clear();
                            }
                            for (int j = 0; j < editListview.getChildCount(); j++){
                                LinearLayout ll = (LinearLayout)editListview.getChildAt(j);
                                LinearLayout tv = ll.findViewById(R.id.main_edit_stock_layout);
                                tv.setBackgroundColor(getResources().getColor(R.color.white));
                            }
                            clauseChooseLayout.setText(getString(R.string.select_all));

                        } else {
                            mDatas.get(i).isCheck = true;
                            allChooseCb.setChecked(true);
                            if (symbolList != null) {
                                symbolList.add(mDatas.get(i).full_symbol);
                            }
                            for (int j = 0; j < editListview.getChildCount(); j++){
                                LinearLayout ll = (LinearLayout)editListview.getChildAt(j);
                                LinearLayout tv = ll.findViewById(R.id.main_edit_stock_layout);
                                tv.setBackgroundColor(getResources().getColor(R.color.transparent_ffe9f0fa));
                            }
                            clauseChooseLayout.setText(getString(R.string.cancel));

                        }

                    }
                    Log.e("全选长度是", symbolList.size() + "");
                    editStockAdapter.notifyDataSetChanged();
                }
                break;

            case R.id.select_all_layout://全选
                chooseType = "2";

                if (!editStockAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        if (mDatas.get(i).isCheck) {
                            mDatas.get(i).isCheck = false;
                            allChooseCb.setChecked(false);
                            if (symbolList != null) {
                                symbolList.clear();
                            }
                            for (int j = 0; j < editListview.getChildCount(); j++){
                                LinearLayout ll = (LinearLayout)editListview.getChildAt(j);
                                LinearLayout tv = ll.findViewById(R.id.main_edit_stock_layout);
                                tv.setBackgroundColor(getResources().getColor(R.color.white));
                            }
                            clauseChooseLayout.setText(getString(R.string.select_all));

                        } else {
                            mDatas.get(i).isCheck = true;
                            allChooseCb.setChecked(true);
                            if (symbolList != null) {
                                symbolList.add(mDatas.get(i).full_symbol);
                            }
                            for (int j = 0; j < editListview.getChildCount(); j++){
                                LinearLayout ll = (LinearLayout)editListview.getChildAt(j);
                                LinearLayout tv = ll.findViewById(R.id.main_edit_stock_layout);
                                tv.setBackgroundColor(getResources().getColor(R.color.transparent_ffe9f0fa));
                            }
                            clauseChooseLayout.setText(getString(R.string.cancel));

                        }

                    }
                    Log.e("全选长度是", symbolList.size() + "");
                    editStockAdapter.notifyDataSetChanged();

                }
                break;

            case R.id.delete_txt://sh
                if (symbolList.size() == 0) {
                    ToastUtil.showToast(getString(R.string.please_select_stock));
                } else {
                    DeleteOptionalStock();
                }
                break;
            case R.id.top_right:
                if (symbolList != null) {
                    symbolList.clear();
                }
                boolean isShow = editStockAdapter.isShow();
                if (isShow) {
                    editTxt.setText(getString(R.string.edit));
                    editStockBottomLayout.setVisibility(View.GONE);
                }else {
                    editTxt.setText(getString(R.string.complete));
                    editStockBottomLayout.setVisibility(View.VISIBLE);
                }
                editStockAdapter.setShow(!isShow);
                editStockAdapter.notifyDataSetChanged();
                for (int j = 0; j < editListview.getChildCount(); j++){
                    LinearLayout ll = (LinearLayout)editListview.getChildAt(j);
                    LinearLayout tv = ll.findViewById(R.id.main_edit_stock_layout);
                    tv.setBackgroundColor(getResources().getColor(R.color.white));
                }
                if (!editStockAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        mDatas.get(i).isCheck = false;
                    }
                    editStockAdapter.notifyDataSetChanged();
                }
                allChooseCb.setChecked(false);
                clauseChooseLayout.setText(getString(R.string.select_all));
                Log.e("全选长度是", symbolList.size() + "");
                break;

            default:
                break;
        }
    }


    //Delete
    private void DeleteOptionalStock() {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < symbolList.size(); i++) {
            jsonArray.put(symbolList.get(i));
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("is_del", "1");
            jsonObject.put("symbol_list", jsonArray);
            OkHttpUtils.getInstance().postWithMemberToken(Url.SAVEOPTIONAL, jsonObject.toString(),  new OkHttpUtils.XCallBack() {

                @Override
                public void onResponse(String result) {
                    if (!StringUtils.isEmpty(result)) {
                        try {
                            final JSONObject jsonObject = new JSONObject(result);
                            if (null != jsonObject && !isFinishing()) {
                                if ("1000".equals(jsonObject.optString("status"))) {
                                    if (chooseType.equals("1")) {
                                        getOptional();
                                        EventBus.getDefault().post(new RfreshQutoes());
                                        EventBus.getDefault().post(new RfreshIndex());
                                        ToastUtil.showToast(getString(R.string.delete_success));
                                    } else {
                                        EventBus.getDefault().post(new RfreshQutoes());
                                        EventBus.getDefault().post(new RfreshIndex());
                                        ToastUtil.showToast(getString(R.string.delete_success));
                                        finish();
                                    }
                                } else {
                                    ToastUtil.showToast(jsonObject.optString("message"));
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        if (symbolList != null) {
            symbolList.clear();
        }
        super.onDestroy();
    }
}
